//
//  ZCBaiduViewController.m
//  03地图定位
//
//  Created by 张聪 on 16/7/24.
//  Copyright © 2016年 张聪. All rights reserved.
//

#import "ZCBaiduViewController.h"
#import "ZCBaiduSerchView.h"
#import "ZCBaiduControllerView.h"
#import "Masonry.h"
#import "ZCBaiduControllerView.h"
#import "ZCSerchViewController.h"
#import "ZCRouteViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件
#import <BaiduMapAPI_Map/BMKMapView.h>



@interface RouteAnnotation : BMKPointAnnotation
{
    int _type; ///<0:起点 1：终点 2：公交 3：地铁 4:驾乘 5:途经点
    int _degree;
}

@property (nonatomic) int type;
@property (nonatomic) int degree;
@end

@implementation RouteAnnotation

@synthesize type = _type;
@synthesize degree = _degree;
@end


@interface ZCBaiduViewController ()<BMKMapViewDelegate, BMKPoiSearchDelegate,BMKLocationServiceDelegate, BMKRouteSearchDelegate, ZCBaiduControllerViewDelegate, ZCSerchViewControllerDelegate, ZCRouteViewControllerDelegate, ZCBaiduSerchViewDelegate, BMKGeoCodeSearchDelegate>
//百度地图
@property (nonatomic, strong) BMKMapView *mapView;

//兴趣点的搜索类
@property (nonatomic, strong) BMKPoiSearch *searcher;

//定位服务
@property (nonatomic, strong) BMKLocationService * locService;

//用户位置
@property (nonatomic, strong) BMKUserLocation *userLocation;

//控制view
@property (nonatomic, weak) ZCBaiduControllerView *baiDuControlView;

//百度反地理编码者
@property (nonatomic, strong) BMKGeoCodeSearch *geoCodeSearch;

//路线的搜索类
@property (nonatomic, strong) BMKRouteSearch *routeSearch;

//是否开启热力图
@property (nonatomic, assign) BOOL isHotMap;

//折线
@property (nonatomic, strong) BMKPolyline* polyLine;

//大头针的标题
@property (nonatomic, copy) NSString *annoTitle;
@property (nonatomic, copy) NSString *annoSubTitle;

//反地理地理编码
@property (nonatomic, strong) BMKReverseGeoCodeOption *reverseGeoCodeOption;

@end

@implementation ZCBaiduViewController
//{
//    BMKMapView *_mapView;
//    BMKPoiSearch *_searcher;
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self settingUI];
    
    [self settingMap];
    
    self.isHotMap = YES;
    
}

#pragma mark - 设置地图
- (void)settingMap {
    //设置用户位置
    self.userLocation = [[BMKUserLocation alloc] init];
    
    //初始化BMKLocationService
    BMKLocationService * locService = [[BMKLocationService alloc]init];
    self.locService = locService;
    
    self.locService.delegate = self;
    //设定定位的最小更新距离，这里设置 200m 定位一次，频繁定位会增加耗电量
    self.locService.distanceFilter = 200;
    
    //设置精准度
    self.locService.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    
    
    
    //启动LocationService
    [self.locService startUserLocationService];
    
    self.mapView.showsUserLocation = NO;//显示定位图层
//    [self.mapView updateLocationData:self.userLocation];
    
    //放大级别
    self.mapView.zoomLevel = 12;
    ////设置定位的状态
    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;
    
    //俯视图
//    self.mapView.overlookEnabled = YES;
    
    //显示比例尺
    self.mapView.showMapScaleBar = YES;
    
}


#pragma mark - settingUI
- (void)settingUI {
    ZCBaiduSerchView *serchView = [[ZCBaiduSerchView alloc] init];
    //设置代理
    serchView.baiduSerchViewDelegate = self;

    [self.view addSubview:serchView];
    [serchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
        
    }];
    
    
    ZCBaiduControllerView *baiDuControlView = [[ZCBaiduControllerView alloc] init];
    self.baiDuControlView = baiDuControlView;
    //设置代理
    baiDuControlView.BaiduControllerViewDelegate = self;
    
    [self.view addSubview:baiDuControlView];
    [baiDuControlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.height.mas_equalTo(120);
    }];
    
    BMKMapView* mapView = [[BMKMapView alloc]init];
    self.mapView = mapView;
    [self.view addSubview:mapView];
    
    
    [mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(serchView.mas_bottom);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.bottom.equalTo(baiDuControlView.mas_top);
        
    }];
    
}

#pragma mark - 事件处理
//定位返回
- (void)againPosition {
    
    //启动LocationService
    [self didUpdateBMKUserLocation:self.userLocation];
    
}


#pragma mark - 代理方法
//导航当前位置到哪去(驾乘)
- (void)didGuidBtn:(NSString *)endName cityName:(NSString *)cityname{
    
    self.routeSearch = [[BMKRouteSearch alloc] init];
    self.routeSearch.delegate = self;
    //起点
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.pt = self.userLocation.location.coordinate;
    //终点
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.name = endName;
    
    BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
    //在那个城市搜索
    transitRouteSearchOption.city = cityname;
    //从什么 -> 到那去
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;
    
    BOOL flag = [self.routeSearch transitSearch:transitRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"bus检索发送成功");
    }
    else
    {
        NSLog(@"bus检索发送失败");
    }
    [self.view endEditing:YES];
    
    [self.view endEditing:YES];
}


//开启热力图
- (void)stratHotMap:(UIButton *)sender {
    
    if (self.isHotMap == YES) {
        
        [sender setTitle:@"关闭热力图" forState:UIControlStateNormal];
        
        [_mapView setBaiduHeatMapEnabled:YES];
        self.isHotMap = NO;
        
        return;
    }
    
    
    if (self.isHotMap == NO) {
        
        [sender setTitle:@"开启热力图" forState:UIControlStateNormal];
        
        [_mapView setBaiduHeatMapEnabled:NO];
        self.isHotMap = YES;
        
    }
}


//切换地图
- (void)didMapChangeClick:(UISegmentedControl *)sender {
    
    switch (sender.selectedSegmentIndex) {
        case 0:
            [_mapView setMapType:BMKMapTypeStandard];
            [_mapView setTrafficEnabled:NO];
            
            break;
        case 1:
            [_mapView setMapType:BMKMapTypeSatellite];
            break;
        case 2:
            [_mapView setTrafficEnabled:YES];
            break;
    
        default:
            break;
    }

    
}

//点击了公交
- (void)didBusClick:(NSString *)startAddessName endAddess:(NSString *)endAddessName stratSerchCity:(NSString *)stratCityName endSerchCity:(NSString *)endCityName{
    
    self.routeSearch = [[BMKRouteSearch alloc] init];
    self.routeSearch.delegate = self;
    //起点
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.name = startAddessName;
    //终点
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.name = endAddessName;
    
    BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
    //在那个城市搜索
    transitRouteSearchOption.city = stratCityName;
    //从什么 -> 到那去
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;
    
    BOOL flag = [self.routeSearch transitSearch:transitRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"bus检索发送成功");
    }
    else
    {
        NSLog(@"bus检索发送失败");
    }
    [self.view endEditing:YES];
    
}
//点击了驾乘
- (void)didCarClick:(NSString *)startAddessName endAddess:(NSString *)endAddessName stratSerchCity:(NSString *)stratCityName endSerchCity:(NSString *)endCityName {
    
    self.routeSearch = [[BMKRouteSearch alloc] init];
    self.routeSearch.delegate = self;
    //起点
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.cityName = stratCityName;
    start.name = startAddessName;
    
    //终点
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.cityName = endCityName;
    end.name = endAddessName;
    
    
    BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init];
    //从什么 -> 到那去
    
    drivingRouteSearchOption.from = start;
    drivingRouteSearchOption.to = end;
    
    BOOL flag = [self.routeSearch drivingSearch:drivingRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"car检索发送成功");
    }
    else
    {
        NSLog(@"car检索发送失败");
    }
    
   [self.view endEditing:YES];
}
//点击了步行
- (void)didWalkClick:(NSString *)startAddessName endAddess:(NSString *)endAddessName stratSerchCity:(NSString *)stratCityName endSerchCity:(NSString *)endCityName {
    
    self.routeSearch = [[BMKRouteSearch alloc] init];
    self.routeSearch.delegate = self;
    //起点
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.cityName = stratCityName;
    start.name = startAddessName;
    
    //终点
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.cityName = endCityName;
    end.name = endAddessName;
    
    
    BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
    //从什么 -> 到那去
    walkingRouteSearchOption.from = start;
    walkingRouteSearchOption.to = end;
    
    BOOL flag = [self.routeSearch walkingSearch:walkingRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"步行检索发送成功");
    }
    else
    {
        NSLog(@"步行检索发送失败");
    }
    [self.view endEditing:YES];
}

//路径规划
- (void)didRouteClick {
    
    ZCRouteViewController *routeVc = [[ZCRouteViewController alloc] init];
    routeVc.RouteViewControllerDelegate = self;
    
    [self presentViewController:routeVc animated:YES completion:nil];
    
    
}

//兴趣点搜索
- (void)serchInterest:(NSString *)nameText {
    
    //移除折线
    [self.mapView removeOverlay:self.polyLine];
    
    
    self.searcher = [[BMKPoiSearch alloc] init];
    
    self.searcher.delegate = self;
    
    //发起搜索
    BMKNearbySearchOption *options = [[BMKNearbySearchOption alloc] init];
    //分组
    options.pageIndex = 0;
    options.pageCapacity = 10;
    options.location = self.userLocation.location.coordinate;
    
    options.keyword = nameText;
    
    BOOL flag = [self.searcher poiSearchNearBy:options];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }
    
//    NSLog(@"%@",nameText);
    
    
    
    [self.view endEditing:YES];
    
}


//控制器的跳转
- (void)pushSerchVc {
    
    ZCSerchViewController *serchVc = [[ZCSerchViewController alloc] init];
    //设置代理
    serchVc.SerchViewControllerDelegate = self;
    
    
    
    [self.navigationController pushViewController:serchVc animated:YES];
    
}

#pragma mark - 添加大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorRed;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

#pragma mark - 添加折线代理方法
- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
        polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        polylineView.lineWidth = 3.0;
        return polylineView;
    }
    return nil;
}

#pragma mark - BMKRouteSearchDelegate
//公交调用
- (void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error {
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0];
        // 计算路线方案中的路段数目
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKTransitStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.instruction;
            item.type = 3;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKTransitStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        
        self.polyLine = polyLine;
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
        
    }

}

- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
        // 计算路线方案中的路段数目
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        // 添加途经点
        if (plan.wayPoints) {
            for (BMKPlanNode* tempNode in plan.wayPoints) {
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item = [[RouteAnnotation alloc]init];
                item.coordinate = tempNode.pt;
                item.type = 5;
                item.title = tempNode.name;
                [_mapView addAnnotation:item];
            }
        }
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        self.polyLine = polyLine;
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
}


- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        self.polyLine = polyLine;
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
}


//根据polyline设置地图范围
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
    CGFloat ltX, ltY, rbX, rbY;
    if (polyLine.pointCount < 1) {
        return;
    }
    BMKMapPoint pt = polyLine.points[0];
    ltX = pt.x, ltY = pt.y;
    rbX = pt.x, rbY = pt.y;
    for (int i = 1; i < polyLine.pointCount; i++) {
        BMKMapPoint pt = polyLine.points[i];
        if (pt.x < ltX) {
            ltX = pt.x;
        }
        if (pt.x > rbX) {
            rbX = pt.x;
        }
        if (pt.y > ltY) {
            ltY = pt.y;
        }
        if (pt.y < rbY) {
            rbY = pt.y;
        }
    }
    BMKMapRect rect;
    rect.origin = BMKMapPointMake(ltX , ltY);
    rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
    [_mapView setVisibleMapRect:rect];
    _mapView.zoomLevel = _mapView.zoomLevel - 0.3;
}


#pragma mark - PoiSearchDeleage
//实现PoiSearchDeleage处理回调结果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        for (BMKPoiInfo *info in poiResultList.poiInfoList) {
            // 添加一个PointAnnotation
            BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
        
            CLLocationCoordinate2D coor = info.pt;
            annotation.coordinate = coor;
            annotation.title = info.name;
            annotation.subtitle = info.address;
            [self.mapView addAnnotation:annotation];
            
            
        }
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果，但在其他城市找到结果时，回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    } else {
        NSLog(@"抱歉，未找到结果");
    }
}


#pragma mark - BMKLocationServiceDelegate 用户位置更新后，会调用此函数
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {
    [_mapView updateLocationData:userLocation];// 动态更新我的位置数据
    BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
    reverseGeoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
    [_geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
}

//1.获取位置信息
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    self.userLocation = userLocation;
    [_mapView updateLocationData:userLocation];// 动态更新我的位置数据
    [_mapView setCenterCoordinate:userLocation.location.coordinate];// 当前地图的中心点
    /// geo检索信息类,获取当前城市数据
    BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
    
    reverseGeoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
    [self.geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
}

//2.地图更新调用反地理编码
#pragma mark BMKMapViewDelegate
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    
    if (self.geoCodeSearch==nil) {
        //初始化地理编码类
        self.geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
        self.geoCodeSearch.delegate = self;
        
    }
    if (self.reverseGeoCodeOption == nil) {
        
        //初始化反地理编码类
        self.reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
    }
    
    //需要逆地理编码的坐标位置
    self.reverseGeoCodeOption.reverseGeoPoint = self.userLocation.location.coordinate;
    //调用反地理编码来获取用户当前位置信息
    [self.geoCodeSearch reverseGeoCode:self.reverseGeoCodeOption];
    
}

#pragma mark BMKGeoCodeSearchDelegate反地理编码
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    //获取周边用户信息
    if (error==BMK_SEARCH_NO_ERROR) {
        
        for(BMKPoiInfo *poiInfo in result.poiList) {

            self.annoTitle = poiInfo.name;
            self.annoSubTitle = poiInfo.address;
            
        }
    }else{
        
        NSLog(@"BMKSearchErrorCode: %u",error);
    }
    
}


#pragma mark - 声明周期
//已经显示
- (void)viewDidAppear:(BOOL)animated {
    
    [_mapView viewWillDisappear];
        //添加大头针
        BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
        annotation.coordinate = self.userLocation.location.coordinate;
        annotation.title = self.annoTitle;
        annotation.subtitle = self.annoSubTitle;
        [_mapView addAnnotation:annotation];
   
}

//将要显示
-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    
    //移除折线
    [self.mapView removeOverlay:self.polyLine];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

//将要消失
-(void)viewWillDisappear:(BOOL)animated {
    //移除上一次的搜索
    [self.mapView removeAnnotations:self.mapView.annotations];
    
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    self.searcher.delegate = nil;
    self.locService.delegate = nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc {
    
    NSLog(@"ZCBaiduViewController 销毁了");
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
